gusucode.com > 产生一个三维的箭头源码程序 > 产生一个三维的箭头源码程序/3D arrow plot/arrow3d.m
function [h]=arrow3d(x,y,z,head_frac,radii,radii2,colr) % % The function plotting 3-dimensional arrow % % h=arrow3d(x,y,z,head_frac,radii,radii2,colr) % % The inputs are: % x,y,z = vectors of the starting point and the ending point of the % arrow, e.g.: x=[x_start, x_end]; y=[y_start, y_end];z=[z_start,z_end]; % head_frac = fraction of the arrow length where the head should start % radii = radius of the arrow % radii2 = radius of the arrow head (defult = radii*2) % colr = color of the arrow, can be string of the color name, or RGB vector (default='blue') % % The output is the handle of the surfaceplot graphics object. % The settings of the plot can changed using: set(h, 'PropertyName', PropertyValue) % % example #1: % arrow3d([0 0],[0 0],[0 6],.5,3,4,[1 0 .5]); % example #2: % arrow3d([2 0],[5 0],[0 -6],.2,3,5,'r'); % example #3: % h = arrow3d([1 0],[0 1],[-2 3],.8,3); % set(h,'facecolor',[1 0 0]) % % Written by Moshe Lindner , Bar-Ilan University, Israel. % July 2010 (C) if nargin==5 radii2=radii*2; colr='blue'; elseif nargin==6 colr='blue'; end if size(x,1)==2 x=x'; y=y'; z=z'; end x(3)=x(2); x(2)=x(1)+head_frac*(x(3)-x(1)); y(3)=y(2); y(2)=y(1)+head_frac*(y(3)-y(1)); z(3)=z(2); z(2)=z(1)+head_frac*(z(3)-z(1)); r=[x(1:2)',y(1:2)',z(1:2)']; N=50; dr=diff(r); dr(end+1,:)=dr(end,:); origin_shift=(ones(size(r))*(1+max(abs(r(:))))+[dr(:,1) 2*dr(:,2) -dr(:,3)]); r=r+origin_shift; normdr=(sqrt((dr(:,1).^2)+(dr(:,2).^2)+(dr(:,3).^2))); normdr=[normdr,normdr,normdr]; dr=dr./normdr; Pc=r; n1=cross(dr,Pc); normn1=(sqrt((n1(:,1).^2)+(n1(:,2).^2)+(n1(:,3).^2))); normn1=[normn1,normn1,normn1]; n1=n1./normn1; P1=n1+Pc; X1=[];Y1=[];Z1=[]; j=1; for theta=([0:N])*2*pi./(N); R1=Pc+radii*cos(theta).*(P1-Pc) + radii*sin(theta).*cross(dr,(P1-Pc)) -origin_shift; X1(2:3,j)=R1(:,1); Y1(2:3,j)=R1(:,2); Z1(2:3,j)=R1(:,3); j=j+1; end r=[x(2:3)',y(2:3)',z(2:3)']; dr=diff(r); dr(end+1,:)=dr(end,:); origin_shift=(ones(size(r))*(1+max(abs(r(:))))+[dr(:,1) 2*dr(:,2) -dr(:,3)]); r=r+origin_shift; normdr=(sqrt((dr(:,1).^2)+(dr(:,2).^2)+(dr(:,3).^2))); normdr=[normdr,normdr,normdr]; dr=dr./normdr; Pc=r; n1=cross(dr,Pc); normn1=(sqrt((n1(:,1).^2)+(n1(:,2).^2)+(n1(:,3).^2))); normn1=[normn1,normn1,normn1]; n1=n1./normn1; P1=n1+Pc; j=1; for theta=([0:N])*2*pi./(N); R1=Pc+radii2*cos(theta).*(P1-Pc) + radii2*sin(theta).*cross(dr,(P1-Pc)) -origin_shift; X1(4:5,j)=R1(:,1); Y1(4:5,j)=R1(:,2); Z1(4:5,j)=R1(:,3); j=j+1; end X1(1,:)=X1(1,:)*0 + x(1); Y1(1,:)=Y1(1,:)*0 + y(1); Z1(1,:)=Z1(1,:)*0 + z(1); X1(5,:)=X1(5,:)*0 + x(3); Y1(5,:)=Y1(5,:)*0 + y(3); Z1(5,:)=Z1(5,:)*0 + z(3); h=surf(X1,Y1,Z1,'facecolor',colr,'edgecolor','none'); camlight lighting phong